package com.example.mybatisdemo2.mapper;

import com.example.mybatisdemo2.model.Student;
import com.example.mybatisdemo2.model.User;
import com.example.mybatisdemo2.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface UserInfoMapper {
    //查询操作
    @Select("select username, `password`, age, gender, phone from userinfo where username= #{username} ")
    public List<UserInfo> queryAllUser(String username);
    @Select("select username, `password`, age, gender, phone from userinfo where username= '${username}' ")
    public List<UserInfo> queryByName(String username);

    @Select("select id,username, `password`, age, gender, phone from userinfo order by id ${sort}")
    public List<UserInfo> sortForRecords(String sort);

    @Select("select id, name, age from user where name like concat('%', #{param}, '%')")
    public List<User> queryByLike(String param);

    //增加操作
    //返回主键
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into userinfo (username, password, age, gender, phone) values" +
            " (#{username}, #{password}, #{age}, #{gender}, #{phone})")
    public Integer insertUser(UserInfo userInfo);

    @Select("select stu.id, stu.name, stu.age, cl.classes_name" +
            " from classes cl left JOIN student stu on cl.id = stu.classes_id where stu.id = #{id}")
    public List<Student> queryById(Integer id);
}
